package com.joke.auto;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jdbc.mysql.MysqlLocalConnect;
import com.jdbc.mysql.MysqlServerConnect;
import com.joke.util.Const;
import com.joke.util.DateUtil;
import com.joke.util.DownloadUtil;
import com.joke.util.HttpClientGet;
import com.joke.util.StringUtil;

public class FetchTxT {
	
	private static final Logger logger = LoggerFactory.getLogger(FetchTxT.class);
	
//	private static String neihanTxt = "http://neihanshequ.com/";
	private static String qiubaiTxt = "https://www.qiushibaike.com/text/page/(*)/";
	private static String baisiTxt = "http://www.budejie.com/text/(*)";
	
	private static String qbCookie = "_ga=GA1.2.1549135057.1520475101; _qqq_uuid_=\"2|1:0|10:1520475372|10:_qqq_uuid_|56:ZTFkNmFlZjhkYzA2N2FiYjE4NmE1ZjJiN2U1MDhlYzY2OWI2ZGE4YQ==|e5a0b4a20205eef24dff2b9e5319748f9db7583be0a947b84c15fae472c4ec16\"; Hm_lvt_18a964a3eb14176db6e70f1dd0a3e557=1520475389; Hm_lvt_743362d4b71e22775786fbc54283175c=1521187836,1521691216; UM_distinctid=1624bdd49a1eb-0505b761b2d4ef-2b0c3866-3d10d-1624bdd49a2276; CNZZDATA1262682741=1214074336-1521689933-%7C1521702324; _xsrf=2|dfd5cdea|42785cf834b69a2cc19cfd269c027318|1521799206; Hm_lvt_2670efbdd59c7e3ed3749b458cafaa37=1521167769,1521167928,1521534546,1521799206; _gid=GA1.2.1851855991.1521799206; __cur_art_index=1602; Hm_lpvt_2670efbdd59c7e3ed3749b458cafaa37=1521799944";
//	private static String nhCookie = "uuid=w:8bed58b103924bd0b6c0573c4d06178a; tt_webid=6533418044445345284; csrftoken=42abd270aa6a66f93839543382dae43c; _gid=GA1.2.493705204.1521179989; _ga=GA1.2.78462588.1521179989";
	private static String bsCookie = "_ga=GA1.2.605736126.1520487304; bfd_g=a0c202420a01680a00003d810001a5ee5aa0cb88; bu=\"2018-03-21 14:42:29\"; baifen_cookie=065085762cd311e8bf80b82a72dd382c; _gid=GA1.2.545856828.1521797615; Hm_lvt_7c9f93d0379a9a7eb9fb60319911385f=1520909693,1521614433,1521691124,1521797615; bfd_s=43102518.43181056.1521797614828; tma=43102518.25831978.1520487303646.1521691124423.1521797614835.7; tmc=4.43102518.41554419.1521797614829.1521797682930.1521797983453; tmd=49.43102518.25831978.1520487303646.; Hm_lpvt_7c9f93d0379a9a7eb9fb60319911385f=1521797984";

	private static String element = null;
	private static String attribute = null;
	private static String value = null;
	

	private Date day = new Date();
	private String date = DateUtil.formatString(day, "yyyyMM");
/*	private String Y = DateUtil.formatString(day, "yyyy");
	private String M = DateUtil.formatString(day, "MM");
	private String D = DateUtil.formatString(day, "dd");
	private String ymd = Y+"/"+M+"/"+D;*/
	String writeTxTPath=Const.fabu+date+Const.localTxt;
	
	public static void main(String[] args) {
		new FetchTxT().fetch();
	}

	public void fetch() {
		Map<String, String> txtMap = new HashMap<>();
		qiubaiTxT(txtMap);
		baisiTxT(txtMap);
		Object[] txt = txtMap.keySet().toArray();
		logger.info("抓取的<段子>条数："+txt.length);
/*		for (int i = 0; i < txt.length; i++) {
			System.out.println(txt[i]);
		}*/
		//保存数据库  本地和服务器
		MysqlServerConnect.insertTxT(txt);
		//保存入库段子
		DownloadUtil.writeTxT(writeTxTPath,txt);
	}
	
	private Map<String, String> qiubaiTxT(Map<String, String> txtMap) {
		for (int i = 1; i < 2; i++) {
			String contentHTML = getContent(qiubaiTxt,qbCookie,i);
			element = "div";
			attribute  = "class";
			value = "content";
			parseContent(contentHTML,txtMap);
		}
		return txtMap;
	}

	private Map<String, String> baisiTxT(Map<String, String> txtMap) {
		for (int i = 1; i < 2; i++) {
			String contentHTML = getContent(baisiTxt,bsCookie,1);
			element = "div";
			attribute  = "class";
			value = "j-r-list-c-desc";
			parseContent(contentHTML,txtMap);
		}
		return txtMap;
	}
/*	private Map<String, String> neihanTxT(Map<String, String> txtMap) {
		for (int i = 1; i < 2; i++) {
			String contentHTML = getContent(neihanTxt,nhCookie,-1);
			element = "h1";
			attribute  = "class";
			value = "title";
			parseContent(contentHTML,txtMap);
		}
		return txtMap;
	}*/

	private String getContent(String url,String cookie, int i) {
		HttpClientGet.createDefaultHttpClient();
		Map<String, String> header = new HashMap<>();
		header.put("Cookie",cookie);
		HttpClientGet.setHeader(header);
		if(i==0) return null;
		else if(i>0) url = url.replace("(*)", String.valueOf(i));
		String contentHTML = HttpClientGet.sendHttpGet(url);
		return contentHTML;
	}

	private Map<String, String> parseContent(String contentHTML,Map<String, String> txtMap) {
		if(contentHTML==null||"".equals(contentHTML)) return txtMap;
		Document doc = Jsoup.parse(contentHTML);  
		Elements links = doc.select(element); 
		String linkHref = null; 
		String content = null; 
    	MysqlLocalConnect.getConnect();
		for (Element element : links) {
			linkHref = element.attr(attribute); 
			if(value.equals(linkHref) ) {
				content = element.text().trim();
				//保存数据库
				if (StringUtil.check(content)&&content.length()>40&&MysqlLocalConnect.query(content, "")) {
					txtMap.put(StringUtil.reSpecial(content), "");
				}
			}
		}
		MysqlLocalConnect.close();
		return txtMap;
	}
	
}
